package com.luoying.itew.http;


import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.gson.JsonObject;
import com.luoying.itew.bean.UserInfo;
import com.luoying.itew.common.AntaiApplication;
import com.luoying.itew.http.rxjava.RxAppApi;
import com.luoying.itew.util.LogUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class HttpRequestUtils {
    private static HttpRequestUtils instance;
    /**接口声明**/
    private AppApi appApi;

    private RxAppApi rxAppApi;

    private HttpRequestUtils(){
        initClent();
    }

    public static HttpRequestUtils getInstance() {
        if(instance==null){
            synchronized (HttpRequestUtils.class){
                if(instance==null){
                    instance = new HttpRequestUtils();
                }
            }
        }
        return instance;
    }

    public RxAppApi getRxAppApi() {
        return rxAppApi;
    }

    /**
     * 初始化请求框架
     */
    private void initClent(){
        //创建日志拦截器
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 创建 OKHttpClient
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addNetworkInterceptor(logInterceptor)
                .addNetworkInterceptor(new StethoInterceptor())
                //连接超时时间
                .connectTimeout(30, TimeUnit.SECONDS)
                //写操作 超时时间
                .writeTimeout(30,TimeUnit.SECONDS)
                //读操作超时时间
                .readTimeout(30,TimeUnit.SECONDS);
        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                //增加返回值为String的支持
                .addConverterFactory(ScalarsConverterFactory.create())
                //增加返回值为Gson的支持(以实体类返回)
                .addConverterFactory(GsonConverterFactory.create())
                //对rxjava的支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(HttpConfig.BASE_HOST)
                .build();
        appApi = retrofit.create(AppApi.class);
        rxAppApi = retrofit.create(RxAppApi.class);
    }

    /**
     * 登录接口
     * @param userAccount
     * @param userPwd
     */
    public void userLogin(String userAccount,String userPwd,HttpResponseCallBack<Map> callBack){
       if(appApi==null){
           initClent();
       }
       BaseMap baseMap = new BaseMap()
               .put("uuid", AntaiApplication.android_id)
               .put("username", userAccount)
               .put("password", userPwd)
               .put("grant_type","password");
       baseMap.remove("access_token");
       appApi.userLogin(baseMap).enqueue(callBack);
    }

    /**
     * 获取用户地图数据信息
     */
    public void getUserMapInfo(HttpResponseCallBack<JsonObject> callBack){
       if(appApi==null){
           initClent();
       }
       appApi.userMapInfo(new BaseMap()).enqueue(callBack);
    }

    /**
     * 获取用户地图数据信息
     */
    public void weatherInfo(HttpResponseCallBack<JsonObject> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.getWeatherInfo(new BaseMap()).enqueue(callBack);
    }

    /**
     * 获取用户地图数据信息
     */
    public void getUserInfo(HttpResponseCallBack<UserInfo> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.userInfo(new BaseMap()).enqueue(callBack);
    }

    /**
     * 获取用户图表信息
     */
    public void getCharts(String monitorPointId,int isDateChar,BaseMap map,HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.getCharts(monitorPointId,isDateChar,map).enqueue(callBack);
    }

    /**
     * 获取巡查日志
     */
    public void getLog(String monitorPointId,int pageNo,HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        BaseMap baseMap = new BaseMap()
                .put("pageNo", pageNo);
        appApi.getLOG(monitorPointId,baseMap).enqueue(callBack);
    }

    /**
     * 获取反馈信息
     */
    public void getSignDetails(HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.getSignDetails(new BaseMap()).enqueue(callBack);
    }

    /**
     * 获取监测点详情
     * @param id
     * @param callBack
     */
    public void get3dInfoDetai(String id,HttpResponseCallBack<JsonObject> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.get3dInfo(id,new BaseMap()).enqueue(callBack);
    }

    /**
     * 获取反馈信息
     */
    public void postLog(String monitorPointId,String userId,String json,HttpResponseCallBack<String> callBack){
        if(appApi==null){
            initClent();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        appApi.postLog(monitorPointId,userId,body).enqueue(callBack);
    }

    /**
     * 下载文件方法
     * @param url 文件路径
     * @param callBack  下载回调
     */
    public void downFile(String url, Callback<ResponseBody> callBack ){
        if(appApi==null){
            initClent();
        }
        appApi.down3dFile(url).enqueue(callBack);
    }

    /**
     * 采集器列表
     * @param id
     * @param pageNo
     * @param callBack
     */
    public void collectionList(String id,int pageNo,int limit ,HttpResponseCallBack<JsonObject> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.collectionDataList(id,new BaseMap().put("pageNo",pageNo).put("limit",limit)).enqueue(callBack);
    }


    /**
     * 获取采集器信息
     */
    public void collectDetail(int monitorPointId ,HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.collectDetail(monitorPointId,new BaseMap()).enqueue(callBack);
    }

    /**
     * 版本更新
     */
    public void hourChart(int monitorPointId ,int displacementType,String yearAndMonth,int monitorId,HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        BaseMap baseMap = new BaseMap()
                .put("monitorPointId", monitorPointId)
                .put("displacementType", displacementType)
                .put("yearAndMonth",yearAndMonth)
                .put("monitorId",monitorId);
        appApi.hourChart(baseMap).enqueue(callBack);
    }

    /**
     * 传递当前设备id
     */
    public void updateUUid(){
        if(appApi==null){
            initClent();
        }
        BaseMap baseMap = new BaseMap()
                .put("uuid", AntaiApplication.android_id);
        appApi.updateUUid(baseMap).enqueue(new Callback<BaseResponse<Map>>() {
            @Override
            public void onResponse(Call<BaseResponse<Map>> call, Response<BaseResponse<Map>> response) {
                LogUtils.i("");
            }

            @Override
            public void onFailure(Call<BaseResponse<Map>> call, Throwable t) {

            }
        });
    }

    /**
     * 极光Bind
     */
    public void jiguangBind(HttpResponseCallBack<String> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.jiguangBind(new BaseMap()).enqueue(callBack);
    }

    /**
     * 退出登录
     * @param userId 用户id
     * @param flag 0手动退出登录 1 token失效退出登录
     * @param callBack 回调
     */
    public void breakApp(int userId,int flag,HttpResponseCallBack<String> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.breakApp(new BaseMap(),userId,flag).enqueue(callBack);
    }

    /**
     * 获取图片列表
     * @param monitorPointId 监测点id
     */
    public void getImgList(int monitorPointId,HttpResponseCallBack<Map<String,String>> callBack){
        if(appApi==null){
            initClent();
        }
        BaseMap map = new BaseMap()
                .put("monitorPointId",monitorPointId);
        appApi.getImgList(map).enqueue(callBack);
    }

    /**
     * 获取大气压数据
     * @param monitorPointId 监测点id
     */
    public void getAtmosData(int monitorPointId,String date,HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        BaseMap map = new BaseMap()
                .put("monitorPointId",monitorPointId)
                .put("date",date);
        appApi.getAtmos(map).enqueue(callBack);
    }

    /**
     * 3d模型所有采集器数据
     * @param monitorPointId 监测点id
     * @param callBack
     */
    public void getCollectorList(int monitorPointId, HttpResponseCallBack<JsonObject> callBack){
        if(appApi==null){
            initClent();
        }
        BaseMap map = new BaseMap()
                .put("monitorPointId",monitorPointId);
        appApi.getCollectorList(map).enqueue(callBack);
    }

    /**
     * 测斜仪数据
     * @param callBack
     */
    public void getInclinometerChat(BaseMap map, HttpResponseCallBack<Map> callBack){
        if(appApi==null){
            initClent();
        }
        appApi.getInclinometerChat(map).enqueue(callBack);
    }

}
